<?php



class CheckLoginForm
{
	
	public function checkIfSubmitted()
	{
		require_once("validateLoginInfo.php");	
		require_once("setSessions.php");
		
		$validateLoginInfo = new ValidateLoginInfo();
		$defaultSessionValues = new setSessions();
		
		// if sessions: message, lastEnteredUsername or loggedIn dont exist,
		// this function will set default values.
		$defaultSessionValues->defaultSessionValues();
		
	
	
		 //Check if the form is submitted
	    if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['username']) && !empty($_POST['userpassword']) && $_SESSION['loggedIn'] == FALSE)
	    {
															
			$_SESSION['loggedIn'] = $validateLoginInfo->LoginValidation( $_POST['username'] , $_POST['userpassword']);
			
			
			if(!$_SESSION['loggedIn'])
			{
				$_SESSION['message'] = "Wrong username and/or password";
				
				$_SESSION['lastEnteredUsername'] = $_POST['username'];
			}
			
			elseif($_SESSION['loggedIn'])
			{
				$_SESSION['lastEnteredUsername'] = $_POST['username'];
				
				$_SESSION['message'] = 'You have logged in!';
				// cookies.
			}
	    }
		
		// NO PASSWORD.
		elseif($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['username']) && empty($_POST['userpassword']))
		{
			$_SESSION['message'] = "You haven't entered a password";
			
			$_SESSION['lastEnteredUsername'] = $_POST['username'];
			
		}
		
		// NO USERNAME / NO USERNAME AND PASSWORD
		elseif($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST['username']) && empty($_POST['userpassword']) || 
		   	   $_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST['username']) && !empty($_POST['userpassword']) ) 
		{
			$_SESSION['message'] = "You haven't entered a username";
			$_SESSION['lastEnteredUsername'] = "";
		}
		
	}
	
}
